#include "canvas.h"
#include <iostream>
#include <math.h>

Canvas::Canvas()
{
    set_events(Gdk::ALL_EVENTS_MASK);
    set_extension_events(Gdk::EXTENSION_EVENTS_ALL);
    set_flags(Gtk::CAN_FOCUS);
    set_size_request(400,400);
    mySurface = Gdk::Pixmap::create(mySurface,400,400,24);
    myContext = mySurface->create_cairo_context();
    myContext->set_source_rgb(1,1,1);
    myContext->paint();
}

Canvas::~Canvas()
{
}

bool Canvas::on_expose_event(GdkEventExpose* event)
{
    blit(event->area);
    return 1;
}

void Canvas::blit(GdkRectangle area)
{
    get_window()->draw_drawable (Gdk::GC::create(get_window()), mySurface, area.x, area.y, area.x, area.y, area.width, area.height);
}

bool Canvas::on_key_press_event(GdkEventKey* event)
{
    if (event->keyval == GDK_Delete)
    {
        myContext->set_source_rgb(1,1,1);
        myContext->paint();
        GdkRectangle rect = {0,0,400,400};
        blit(rect);
    }
    return false;
}

bool Canvas::on_motion_notify_event(GdkEventMotion* event)
{
    GdkDevice * device = event->device;
    if (event->state & GDK_BUTTON1_MASK)
    {
        double width = 1;
        if (event->axes)
        {
            width += 15 * event->axes[2];
        }
        myContext->arc(event->x,event->y,width,0,2 * M_PI);
        myContext->set_source_rgb(1,0,0);
        myContext->fill();
        GdkRectangle rect = {event->x - 16, event->y - 16, 32, 32};
        blit(rect);
    }
    return false;
}
